package com.subway.babynote;

/*水*/
import com.subway.babynote.syncservice.syncapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;

public class feedback extends Activity {
	private static final int INVISIBLE = 0x00000004;
	EditText feedback_et;
	Button send_button;
	Button login_button;
	private LinearLayout linearLayout, statistics, sync, more;
	Intent In;
	NotedbHelper notedb;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedback);
		setTitle(R.string.feedbacktitle);
		feedback_et = (EditText) findViewById(R.id.feedback_edit);
		send_button = (Button) findViewById(R.id.send_button);
		login_button = (Button) findViewById(R.id.login_button);
		notedb = new NotedbHelper(this);
		In = new Intent();
		//initTabFunction();
		try {
			if (syncapi.getSyncImpl().getUserid() != -1) {
				login_button.setVisibility(INVISIBLE);
				login_button.setClickable(false);
			}
		} catch (Exception e) {
			Log.v("feedback", "init feedback e=" + e.getMessage());
		}
		
		send_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				String content = feedback_et.getText().toString();
				try {

					int user_id = syncapi.getSyncImpl().getUserid();
					if (user_id == -1) {
						user_id = notedb.getVisitorid();
					}

				//	notedb.AddFeedback(user_id, notedb.nowTime(), content);
				/*	int ret = syncapi.getSyncImpl().informSync(
							syncapi.operation_type.addFeedback.code,
							notedb.nowTime());*/
					int ret=syncapi.getSyncImpl().AddFeedback(content);
					if (ret != 0) {
						Log.v("feedback", "sync to server failed");
					}
				} catch (Exception e) {
					Log.v("feedback", "add feeback e=" + e.getMessage());
				}
				bundle.putString("feedback", feedback_et.getText().toString());

				In.setClass(feedback.this, feedbackfinish.class);
				In.putExtras(bundle);
				startActivity(In);
				feedback.this.finish();
			}
		});

		login_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				In.setClass(feedback.this, login.class);
				startActivity(In);
			}
		});
	}

	

	public void onDestroy() {
		notedb.close();
		super.onDestroy();

	}
}